package com.he.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.he.pojo.Department;
import com.he.pojo.Permission;
import com.he.pojo.PermissionDetail;
import com.he.pojo.Role;
import com.he.pojo.RoleExample;
import com.he.service.department.DepartmentServiceI;
import com.he.service.permission.PermissionServiceI;
import com.he.service.permissionDetail.PermissionDetailServiceI;
import com.he.service.role.RoleServiceI;

@Controller
@RequestMapping("role")
/**
 * 
 * @类名：RoleController
 * @描述：用户职位管理Controller
 * @作者：张赫赫
 * @日期：2018年8月24日 下午4:49:59
 */
public class RoleController {
	@Autowired
	RoleServiceI roleService;
	@Autowired
	DepartmentServiceI departService;
	@Autowired
	PermissionServiceI permissionService;
	@Autowired
	PermissionDetailServiceI detailService;
	@RequestMapping("show")
	public String roleShow(ModelMap map,Role role) {
		//分页参数
		StringBuffer params=new StringBuffer();
		/*通过role*/
		RoleExample example=new RoleExample();
		if(StringUtils.isNotBlank(role.getRoleName())) {
			example.createCriteria().andRoleNameLike("%"+role.getRoleName()+"%");
			params.append("&roleName="+role.getRoleName());
		}
		List<Role> roles = roleService.getRoles(example, role);
		for (Role role2 : roles) {
			Department depart = departService.getDepartmentById(role2.getDeptId());
			role2.setDepart(depart.getDeptName());
		}
		//为前端传递查询结果list
		map.addAttribute("roles", roles);
		/*为role总页数赋值,向前端传总页数*/
		PageInfo<Role> pageInfo=new PageInfo<>(roles);
		//为role总记录数赋值
		role.setTotal(pageInfo.getTotal());
		//为role总页数赋值
		role.setPages(pageInfo.getPages());
		/*为role返回的controller链接赋值*/
		role.setUrl("show.do");
		/*为role查询条件赋值*/
		role.setParams(params.toString());
		/*向前端传参数保存在role中*/
		map.addAttribute("role", role);
		return "PerRole";
	}
	@RequestMapping(value="add",method=RequestMethod.GET)
	public String roleAddGet(ModelMap map) {
		List<Department> departments = departService.getDepartments(null, new Department());
		List<Permission> permissions = permissionService.getPermissions(null, new Permission());
		map.addAttribute("departments", departments);/*向前端传部门信息*/
		map.addAttribute("permissions", permissions);/*向前端传权限信息*/
		return "addPerRole";
	}
	@RequestMapping(value="add",method=RequestMethod.POST)
	public String roleAddPost(Role role) {
		int i = roleService.insert(role);
		//在职位添加之初为所有权限初始化，权限初始化为0
		Permission permission=new Permission();
		permission.setWithPage(0);
		List<Permission> permissions = permissionService.getPermissions(null, permission);//获取权限表
		for (Permission permission2 : permissions) {//遍历所有权限
			PermissionDetail permissionDetail=new PermissionDetail();
			permissionDetail.setPermissionId(permission2.getId());
			permissionDetail.setRoleId(role.getId());//在mapper中使用useGeneratedKeys="true" keyProperty="id",插入后返回id
			permissionDetail.setIsPermission((byte)0);
			detailService.insert(permissionDetail);
		}
		if(i==1) {
			return "redirect:show.do";/*添加成功重定向到show显示所有职位*/
		}
		else {
			return "addPerRole";/*添加失败转发到修改页面，再次添加*/
		}
	}
	@RequestMapping(value="update",method=RequestMethod.GET)
	public String roleUpdateGet(Role role,ModelMap map) {
		Role role2=new Role();
		if(role!=null) {/*role不为空时，转发到职位修改页面*/
			role2 = roleService.getRoleById(role.getId());
			List<Department> departments = departService.getDepartments(null, new Department());
			List<Permission> permissions = permissionService.getPermissions(null, new Permission());
			map.addAttribute("role", role2);
			map.addAttribute("departments", departments);/*向前端传部门信息*/
			map.addAttribute("permissions", permissions);/*向前端传权限信息*/
			return "modPerRole";
		}
		else {/*role为空，重定向到职位显示*/
			return "redirect:show.do";
		}
	}
	@RequestMapping(value="update")
	public String roleUpdatePost(Role role) {
		int i = roleService.update(role);
		if(i==1) {/*修改成功重定向到show显示所有职位*/
			return "redirect:show.do";
		}
		else {/*修改失败转发到修改页面，再次修改*/
			return "modPerRole";
		}
	}
	@RequestMapping("del")
	@ResponseBody
	public String del(int id) {
		/*删除职位，删除是否成功的判断在前端完成*/
		return String.valueOf(roleService.delete(id));
	}
}
